Skip to content

VK: sample depth-stencil textures in the read-only attachment layout - #273

Closed
duckdoom5 wants to merge 1 commit into
NVIDIA-RTX:mainfrom
duckdoom5:vk-depth-readonly-texture-view
Closed

duckdoom5 wants to merge 1 commit into
NVIDIA-RTX:mainfrom
duckdoom5:vk-depth-readonly-texture-view

Conversation

@duckdoom5

Copy link
Copy Markdown
Contributor

I was running into some issues when sampling depth-stencil textures in the read-only attachment layout (for particle systems).

I had Claude look into it and generate two different solutions.

The first added a Vulkan only flag (see change list).

I wasn't sure how you'd feel about that, so I asked it to create a different solution and it came up with this version.
Claude told me this solution might even be "faster than the current", but the upscalers would need a dual barrier to move to the expected layout for the SDK and back to the NRI expected layout.

Here is the PR desc it generated; Let me know which of the two you'd prefer and/or if you know of a better way to fix this.

Vulkan counterpart of #266 (DIRECT_QUEUE_GENERIC_READ on D3D12).

A depth-stencil texture in Layout::SHADER_RESOURCE was in VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, and its TEXTURE views always expected that layout. So a pass could not sample depth it has bound as a read-only attachment (Layout::DEPTH_STENCIL_READONLY, e.g. soft particles or decals): the descriptor layout never matched the image layout (VUID-vkCmdDraw-imageLayout-00344). A pure barrier change cannot fix this on Vulkan, because a depth attachment can’t be in SHADER_READ_ONLY_OPTIMAL and the descriptor carries its own layout.

Change: shader reads of depth-stencil textures now use VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL for both the barrier layout of SHADER_RESOURCE and the descriptor layout of TEXTURE* views. A depth texture in SHADER_RESOURCE and one in DEPTH_STENCIL_READONLY then share one layout, matching D3D12 semantics. No API change.

Upscalers: the FFX (legacy Vulkan backend) and NGX SDKs sample their inputs in VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, so on Vulkan CmdDispatchUpscale moves a depth-stencil depth guide into that layout around the SDK call and back. Callers still pass and receive it in Layout::SHADER_RESOURCE. NIS is unaffected because it uses NRI descriptors.

Open question: is the per-dispatch upscaler bridge acceptable, or would you prefer another approach (e.g. an opt-in view flag)?

Tested: RenderTest (soft particles, FSR) on Vulkan with validation, RTX 3090 Ti. DLSS paths are untested.

A depth-stencil texture in "SHADER_RESOURCE" was in
"VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL" and its "TEXTURE" views expected that layout,
so it could not be sampled while bound as a read-only depth-stencil attachment
("DEPTH_STENCIL_READONLY", e.g. soft particles): the descriptor layout never matched
(VUID-vkCmdDraw-imageLayout-00344). Shader reads of depth-stencil textures now use
"VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL" for both the barrier layout and the
descriptor, the Vulkan counterpart of NVIDIA-RTX#266 ("DIRECT_QUEUE_GENERIC_READ" on D3D12).

SDK-backed upscalers (FSR, DLSR, DLRR) sample their inputs in
"VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL", so CmdDispatchUpscale moves a depth-stencil
depth guide into that layout around the SDK call and back.
dzhdanNV added a commit that referenced this pull request Oct 19, 2026
@dzhdanNV

Copy link
Copy Markdown
Collaborator

Thanks! Sorry for the hassle with read-only stuff. After some thinking I decided to stay explicit, but instead of adding a flag, I reused already existing Layout. Please, review 3d8ca25. It should solve the problem.

@duckdoom5

Copy link
Copy Markdown
Contributor Author

No worries! Thanks for putting in the effort to fix it properly. I'll try your patch some time tomorrow. Will let you know

@dzhdanNV

Copy link
Copy Markdown
Collaborator

No, thanks to you! Sure. Keeping this one open until you confirm that the fix is legit.

@duckdoom5

Copy link
Copy Markdown
Contributor Author

Yes, it works!

@duckdoom5 duckdoom5 closed this Oct 22, 2026
@duckdoom5
duckdoom5 deleted the vk-depth-readonly-texture-view branch October 22, 2026 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants